<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="default" @click="handleExport()">
          <Icon icon="i-ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.hid })">
          {{ record.parent.fbillno }}
        </a>
      </template>

      <!-- <template #combinedProducts="{ record }">
        <div v-html="handlecombinedProducts(record.combinedProducts)"> </div>
      </template> -->
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <FormImport @register="registerImportModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="ViewsWmsRdRd32Rdrecord32ListDetails2">
  import { unref, onMounted, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { rdrecords32SubListData } from '/@/api/wms/rd/rd32/rdrecord32';
  import { companyTreeData } from '/@/api/sys/company';
  import { useGlobSetting } from '/@/hooks/setting';
  import { downloadByUrl } from '/@/utils/file/download';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { Table } from 'ant-design-vue';
  import { FormProps } from '/@/components/Form';
  import InputForm from './formView.vue';
  import FormImport from './formImport.vue';
  import { useUserStore } from '/@/store/modules/user';
  import { dateUtil } from '/@/utils/dateUtil';
  const userStore = useUserStore();

  const { t } = useI18n('rd.rd32.rdrecord32');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('出库任务单管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('公司'),
        field: 'parent.companyCode',
        component: 'TreeSelect',
        componentProps: {
          api: companyTreeData,
          allowClear: true,
          immediate: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('发货单号'),
        field: 'parent.fbillno',
        component: 'Input',
      },
      {
        label: t('创建时间'),
        field: 'parent.createDate',
        // component: 'DatePicker',
        component: 'RangePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          // showTime: { format: 'HH:mm' },
          showTime: false,
          // valueFormat: 'YYYY-MM-DD',
        },
      },
      {
        label: t('订单号'),
        field: 'forderno',
        component: 'Input',
      },
      {
        label: t('紧急程度'),
        field: 'parent.furgentlevel',
        component: 'Select',
        componentProps: {
          dictType: 'wms_jj_level',
          allowClear: true,
        },
      },

      {
        label: t('购货单位名称'),
        field: 'parent.fsuppliername',
        component: 'Input',
      },
      {
        label: t('联系人'),
        field: 'fperson',
        component: 'Input',
      },
      {
        label: t('电话'),
        field: 'fphone',
        component: 'Input',
      },
      {
        label: t('地址'),
        field: 'faddress',
        component: 'Input',
      },
      {
        label: t('发运方式'),
        field: 'parent.fyfs',
        component: 'Input',
      },
      {
        label: t('商品名称'),
        field: 'fitemname',
        component: 'Input',
      },
      {
        label: t('状态'),
        field: 'parent.rwStatus',
        component: 'Select',
        componentProps: {
          dictType: 'rd32_jh_status',
          allowClear: true,
          onChange: handleSuccess,
        },
      },
      {
        label: t('发货日期'),
        field: 'parent.fdate',
        // component: 'DatePicker',
        component: 'RangePicker',
        componentProps: {
          format: 'YYYY-MM-DD',
          // showTime: { format: 'HH:mm' },
          showTime: false,
          // valueFormat: 'YYYY-MM-DD',
        },
      },
      {
        label: t('业务员'),
        field: 'parent.femp',
        component: 'Input',
      },
    ],
    fieldMapToTime: [
      ['parent.createDate', ['parent.createDate_gte', 'parent.createDate_lte']],
      ['parent.fdate', ['parent.fdate_gte', 'parent.fdate_lte']],
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('订单号'),
      dataIndex: 'forderno',
      key: 'a.forderno',
      sorter: true,
      width: 110,
      align: 'left',
    },
    {
      title: t('紧急程度'),
      dataIndex: 'parent.furgentlevel',
      key: 'parent.furgentlevel',
      sorter: true,
      width: 80,
      align: 'left',
      dictType: 'wms_jj_level',
    },
    {
      title: t('创建时间'),
      dataIndex: 'parent.createDate',
      key: 'parent.create_date',
      sorter: true,
      width: 120,
      align: 'center',
    },
    {
      title: t('购货单位名称'),
      dataIndex: 'parent.fsuppliername',
      key: 'parent.fsuppliername',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('联系人'),
      dataIndex: 'fperson',
      key: 'a.fperson',
      sorter: true,
      width: 80,
      align: 'left',
    },
    {
      title: t('电话'),
      dataIndex: 'fphone',
      key: 'a.fphone',
      sorter: true,
      width: 110,
      align: 'left',
    },
    {
      title: t('地址'),
      dataIndex: 'faddress',
      key: 'a.faddress',
      sorter: true,
      width: 230,
      align: 'left',
    },
    {
      title: t('数量'),
      dataIndex: 'fqty',
      key: 'a.fqty',
      sorter: true,
      width: 80,
      align: 'right',
      showSummary: true,
      showTotalSummary: true,
    },
    {
      title: t('重量'),
      dataIndex: 'fweight',
      key: 'a.fweight',
      sorter: true,
      width: 80,
      align: 'right',
      showSummary: true,
      showTotalSummary: true,
    },
    {
      title: t('发运方式'),
      dataIndex: 'parent.fyfs',
      key: 'parent.fyfs',
      sorter: true,
      width: 80,
      align: 'left',
    },
    {
      title: t('公司名称'),
      dataIndex: 'parent.companyName',
      key: 'parent.company_name',
      sorter: true,
      width: 150,
      align: 'left',
    },
    {
      title: t('公司编码'),
      dataIndex: 'parent.companyCode',
      key: 'a.company_code',
      sorter: true,
      width: 60,
      align: 'left',
      ifShow: false,
    },

    {
      title: t('发货单号'),
      dataIndex: 'parent.fbillno',
      key: 'parent.fbillno',
      sorter: true,
      width: 120,
      align: 'left',
      fixed: 'left',
      slot: 'firstColumn',
    },

    {
      title: t('商品名称'),
      dataIndex: 'fitemname',
      key: 'a.fitemname',
      sorter: true,
      width: 300,
      align: 'left',
      // slot: 'combinedProducts',
    },
    {
      title: t('发货日期'),
      dataIndex: 'parent.fdate',
      key: 'parent.fdate',
      sorter: true,
      width: 110,
      align: 'left',
      // slot: 'combinedProducts',
    },
    {
      title: t('业务员'),
      dataIndex: 'parent.femp',
      key: 'parent.femp',
      sorter: true,
      width: 110,
      align: 'left',
      // slot: 'combinedProducts',
    },
    // {
    //   title: t('状态'),
    //   dataIndex: 'parent.rwStatus',
    //   key: 'parent.rw_status',
    //   sorter: true,
    //   width: 80,
    //   align: 'center',
    //   fixed: 'right',
    // },
  ];

  const actionColumn: BasicColumn = {
    width: 160,
    actions: (record: Recordable) => [
      {
        icon: 'i-clarity:note-edit-line',
        title: t('编辑出库任务单'),
        onClick: handleForm.bind(this, { id: record.id }),
        auth: 'rd:rd32:rdrecord32:edit',
      },
      {
        icon: 'i-ant-design:delete-outlined',
        color: 'error',
        title: t('删除出库任务单'),
        popConfirm: {
          title: t('是否确认删除出库任务单'),
          confirm: handleDelete.bind(this, record),
        },
        auth: 'rd:rd32:rdrecord32:edit',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getDataSource, setProps }] = useTable({
    api: rdrecords32SubListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    //actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    immediate: false,
    pagination: {
      pageSizeOptions: [20, 100, 500, 1000, 2000, 5000],
    },
    showCustomSummary: true,
    showTotalCustomSummary: true,
    // ellipsis: false,
  });

  const totalFqty = computed(() => {
    const tableData = getDataSource();
    // const tableData = getDataSource.value;
    let sum = tableData.reduce((total, item) => total + (item.fqty || 0), 0);
    return sum;
  });

  const totalFweight = computed(() => {
    const tableData = getDataSource();
    // const tableData = getDataSource.value;
    let sum = tableData.reduce((total, item) => total + (item.fweight || 0), 0);
    sum = sum.toFixed(2);
    return sum;
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  function handlecombinedProducts(value) {
    let newStr = value.replace(/##/g, '<br>'); // 替换所有'o'为'0'
    return newStr;
  }

  async function handleDelete(record: Recordable) {
    const params = { id: record.id };
    const res = await rdrecord32Delete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url: ctxAdminPath + '/rd/rd32/rdrecord32/exportData',
      params: getForm().getFieldsValue(),
    });
  }

  const [registerImportModal, { openModal: importModal }] = useModal();

  function handleImport() {
    importModal(true, {});
  }

  // 同步K3出库任务单
  async function handleSyn() {
    const res = await rdrecord32Syn({});
    showMessage(res.message);
    handleSuccess({});
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }

  onMounted(async () => {
    setProps();
    handleCompany();
  });

  async function handleCompany() {
    let date = dateUtil(new Date()).format('YYYY-MM-DD');
    let arr = [date, date];
    await getForm().setFieldsValue({
      'parent.companyCode': userStore.getProjecte.code,
      'parent.createDate': arr,
    });
    reload();
    await getForm().setFieldsValue({
      'parent.companyCode': userStore.getProjecte.code,
      'parent.createDate': arr,
    });
  }
</script>
<style scoped>
  /deep/ .ant-pagination-options-size-changer {
    width: 100px !important;
  }
</style>
